Practical-08
Mobile Application
Practical List
Create an android program that will read phonebook contacts using content providers and display them in a list.
Steps
- Create a new Android project in Android Studio.
- Open the
activity_main.xmllayout file. - Add a
ListViewelement to the layout with the following attributes:android:id="@+id/listView"android:layout_width="match_parent"android:layout_height="match_parent"
- Open the
MainActivity.javafile. - Inside the
onCreatemethod, add the following code to set the content view to theactivity_main.xmllayout:setContentView(R.layout.activity_main); - Create a new method called
displayContactsto fetch and display the phonebook contacts:private void displayContacts() {
List<Contact> contactList = getContacts();
ContactListAdapter adapter = new ContactListAdapter(this, contactList);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
} - Create a new class called
Contactto represent a contact object with the following attributes:String nameString phoneNumber
- Create a new class called
ContactListAdapterthat extendsArrayAdapter<Contact>and implements the necessary methods to display the contact items in the list view. - Implement the
getContactsmethod to fetch the phonebook contacts using content providers and return a list of contact objects. - Run the application on an Android device or emulator.
- The list view should display the phonebook contacts with their names and phone numbers.